<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/11/12
 * Time: 下午2:08
 */

namespace Api\Controller;


use Api\Service\AddressService;
use Api\Service\GoodsService;
use Api\Service\OrderService;

class AddressController extends LoginBaseController
{

    //新增收货地址
    public function add(){
        $add['name']=I('post.name');
        if(empty($add['name'])){
            $this->ajax(202,'姓名不能为空');
        }
        $add['mobile']=I('post.mobile');
        if(empty($add['mobile'])){
            $this->ajax(202,'手机号码不能为空');
        }
        if(!is_phone($add['mobile'])){
            $this->ajax(202,'手机号码格式错误');
        }
        $add['province']=I('post.province');
        if(empty( $add['province'])){
            $this->ajax(202,'请输入省份');
        }
        $add['province_id']=I('post.province_id');
        if(empty($add['province_id'])){//省份id传入错误
            $this->ajax(202,'省份id传入错误');
        }
        $add['city']=I('post.city');
        if(empty($add['city'])){
            $this->ajax(202,'城市不能为空');
        }
        $add['county']=I('post.county');//区县
        if(empty($add['county'])){
            $this->ajax(202,'区县不能为空');
        }
        $add['address']=I('post.address');
        if(empty($add['address'])){
            $this->ajax(202,'详细地址不能为空');
        }
        $add['uid']=$this->uid;
        $add['is_default']=I('post.is_default');
        if($add['is_default']==1){//设置为默认地址 修改其他地址状态
            AddressService::S()->saveAddress(array('uid'=>$this->uid,'is_default'=>1),array('is_default'=>0));
        }
        $re=AddressService::S()->add($add);
        if($re){
            $this->ajax('200','success');
        }
    }

    //修改收货地址
    public function saveAddress(){
        $add['name']=I('post.name');
        if(empty($add['name'])){
            $this->ajax(202,'姓名不能为空');
        }
        $add['mobile']=I('post.mobile');
        if(empty($add['mobile'])){
            $this->ajax(202,'手机号码不能为空');
        }
        if(!is_phone($add['mobile'])){
            $this->ajax(202,'手机号码格式错误');
        }
        $add['province']=I('post.province');
        if(empty( $add['province'])){
            $this->ajax(202,'请输入省份');
        }
        $add['province_id']=I('post.province_id');
        $add['city']=I('post.city');
        if(empty($add['city'])){
            $this->ajax(202,'城市不能为空');
        }
        $add['county']=I('post.county');//区县
        if(empty($add['county'])){
            $this->ajax(202,'区县不能为空');
        }
        $add['address']=I('post.address');
        if(empty($add['address'])){
            $this->ajax(202,'详细地址不能为空');
        }
        $add['is_default']=I('post.is_default');
        if($add['is_default']==1){//设置为默认地址 修改其他地址状态
            AddressService::S()->saveAddress(array('uid'=>$this->uid,'is_default'=>1),array('is_default'=>0));
        }
        $addId=I('post.id');
        $re=AddressService::S()->saveAddress(array('id'=>$addId),$add);
        if($re===false){
            $this->ajax(202,'修改失败');
        }else{
            $this->ajax(200,'修改成功');
        }
    }


    //查询出默认地址
    public function findDefault(){
        $where['uid']=$this->uid;
        $where['is_default']=1;
        $re=AddressService::S()->findAddByWhere($where,self::defaultFiled());
        if(!$re){
            $this->ajax(202,'没有数据信息哦');
        }else{
            $this->ajax(200,'查询成功',$re);
        }
    }


    public function defaultFiled(){
        $filed='id,name,mobile,province,province_id,city,county,address,is_default';
        return $filed;
    }


    //查询收货地址
    public function queryAddress(){
        $where['uid']=$this->uid;
        $addressInfo=AddressService::S()->selectAddByWhere($where,self::defaultFiled());
        if(!$addressInfo){
            $this->ajax(202,'没有收货地址哦');
        }else{
           // dump($addressInfo);die;
            foreach ($addressInfo as $k=>$v){
                $whereAdd['parentid']=$v['province_id'];
                $whereAdd['area_name']=$v['city'];
                $city=AddressService::S()->findArea($whereAdd,'area_id');
                $addressInfo[$k]['city_id']=$city['area_id'];
            }
            $this->ajax(200,'查询成功',$addressInfo);
        }
    }

    //删除收货地址
    public function delAddress(){
        $id=I('post.address_id');
        if(empty($id)){
            $this->ajax(202,'ID不能为空');
        }
        $where['uid']=$this->uid;
        $where['id']=$id;
        $addInfo=AddressService::S()->findAddByWhere($where);
        if(empty($addInfo)){
            $this->ajax(202,'收货地址ID错误');
        }
        $re=AddressService::S()->delAddress($id);
        if($re){
            $this->ajax(200,'success');
        }else{
            $this->ajax(202,'error');
        }
    }


    //查询身份
    public function area(){
        $grade=I('post.level');
        $parent_id=I('post.parent_id');
        $where=array();
        if(empty($grade)){
            $where['level']=1;
        }else{
            $where['level']=$grade;
        }
        if(empty($parent_id) || $parent_id==0){
            $where['parentid']=100000;
        }else{
            $where['parentid']=$parent_id;
        }
        $re=AddressService::S()->selectArea($where);
        $this->ajax(200,'success',$re);

    }

    //计算运费
    public function freightMoney(){
        $address_id=I('post.address_id');
        $goods_ids=I('post.goods_ids');
        $goodsNum=I('post.goods_num');
        //查询地址信息
        //$where['uid']=7087;//$this->uid;
        $where['id']=$address_id;
        $address=AddressService::S()->findAddByWhere($where,'province_id');
        if(!$address){
            $this->ajax(202,'地址信息错误');
        }
        $where['goods_id']=array('in',$goods_ids);
        $filed='goods_id,freight_payer,goods_weight';
        $goodsInfo=GoodsService::S()->queryGoodsInfoByWhere($where,$filed);
        foreach ($goodsInfo as $k=>$v){
            $goodsInfo[$k]['num']=$goodsNum[$v['goods_id']];
        }
        //dump($goodsInfo);die;
        $money=OrderService::S()->get_shipping($goodsInfo,$address['province_id']);
        $this->ajaxReturn(array('code'=>200,'msg'=>'success','shipping'=>$money));
        //$this->ajax(200,'success',$money,'shipping');
    }

    /*
     * 计算运费
     * TODO
     */
    public function _get_shipping($goods_info,$aid=330000){
        //计算要支付的钱
        $money = 0;
        $all_weight = 0;//订单总重量
        $s = M("express");
        $where = array(
            "province_id" => $aid
        );
        $res = $s->where($where)->find();
        //首重和续重
        $first = $res["first_weight"];
        $next = $res["next_weight"];
        foreach ($goods_info as $k=>$v){
            //判断是不是包邮商品
            if($v['freight_payer'] == 1){
                continue;
            }

            $all_weight += $v['num'] * $v['goods_weight'];
        }
        $all_weight = ceil($all_weight);

        if($all_weight == 0){
            $money = 0;
        }else if($all_weight<=1){
            $money = $first;
        }else{
            $money = $first + ($all_weight-1)*$next;
        }

        $money = round($money,2);//四舍五入
        return $money;
    }
}